package com.bbx.taxi.client.widget;

import com.bbx.taxi.official.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

public class MyViewPager extends ViewPager {
	private boolean noScroll = false;
	private boolean iswrap = false;// 判断是否自适应试图大小
	private View view;

	public MyViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
		 TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.MyViewPage);
		 iswrap=a.getBoolean(R.styleable.MyViewPage_iswrap, false);
	}

	public MyViewPager(Context context) {
		super(context);
	}

	public void setNoScroll(boolean noScroll) {
		this.noScroll = noScroll;
	}

	@Override
	public void scrollTo(int x, int y) {
		super.scrollTo(x, y);
	}

	@Override
	public boolean onTouchEvent(MotionEvent arg0) {
		/* return false;//super.onTouchEvent(arg0); */
		if (noScroll)
			return false;
		else
			return super.onTouchEvent(arg0);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent arg0) {
		if (noScroll)
			return false;
		else
			return super.onInterceptTouchEvent(arg0);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		if (!iswrap) {
			int height = 0;
			// 下面遍历所有child的高度
			if(view!=null){
				view.measure(widthMeasureSpec,
						MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
			   int h = view.getMeasuredHeight();
			   if(view instanceof AbsListView){
					AbsListView listview=(AbsListView)view;
					h=h*listview.getCount();
				}
			   height = h;
			}else{
				for (int i = 0; i < getChildCount(); i++) {
				View child = getChildAt(i);
				child.measure(widthMeasureSpec,
						MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
				int h = child.getMeasuredHeight();
				if(child instanceof AbsListView){
					AbsListView listview=(AbsListView)child;
					h=h*listview.getCount();
				}
				
				if (h > height) // 采用最大的view的高度。
					height = h;
			}
			}
			heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
					MeasureSpec.EXACTLY);
		}
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
	/**
	 * onPageSelected 调用
	 */
	public void setCurrentView(View view){
		this.view=view;
	}
}